import java.util.concurrent.atomic.AtomicReference;

/**
 * @Auther: zch
 * @Date: 2019/1/8 15:31
 * @Description:
 */
public class AtomicReferenceDemo {

    static AtomicReference<Integer> money = new AtomicReference<Integer>();

    public static void main(String[] args) throws InterruptedException {
        money.set(19);
        for (int i=0;i<3;i++) {
            new Thread(){
                @Override
                public void run() {
                    while (true) {
                        while (true) {
                            Integer m = money.get();
                            if (m < 20) {
                                if (money.compareAndSet(m, m + 20)) {
                                    System.out.println(">>20 success,money:"+money.get());
                                    break;
                                }
                            }else {
                                break;
                            }
                        }
                    }
                }
            }.start();
        }
    }


}
